package com.ruoyi.auth;

import com.ruoyi.auth.service.UnifiedAuthenticationService;
import org.apache.hc.client5.http.classic.HttpClient;
import org.apache.hc.client5.http.impl.classic.HttpClients;
import org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;

/**
 * starter 的自动配置类
 * email：954219492@qq.com
 */
@AutoConfiguration
@EnableConfigurationProperties(UnifiedAuthenticationProperties.class)
public class UnifiedAuthenticationAutoConfiguration {

    @Autowired
    private UnifiedAuthenticationProperties properties;
    // 添加HTTP客户端配置
//    @Bean
//    public HttpClient httpClient() {
//        return HttpClients.custom()
//                .setConnectionManager(new PoolingHttpClientConnectionManager())
//                .build();
//    }

    @Bean
    @ConditionalOnMissingBean(UnifiedAuthenticationService.class)
    public UnifiedAuthenticationService unifiedAuthenticationService(
            AuthenticationConfigurationProvider provider
    ) {  // 添加httpClient参数
//        return new UnifiedAuthenticationService(properties, httpClient);

        return new UnifiedAuthenticationService(provider);
    }

    // 当 AuthenticationConfigurationProvider 不存在时，使用默认的 AuthenticationConfigurationProvider
    @Bean
    @ConditionalOnMissingBean
    public AuthenticationConfigurationProvider authenticationConfigurationProvider() {
        return new DefaultAuthenticationConfigurationProvider(properties);
    }
}
